package com.duoduo.service.impl;

import com.duoduo.context.BaseContext;
import com.duoduo.dto.ShoppingCartDTO;
import com.duoduo.entity.Dish;
import com.duoduo.entity.Setmeal;
import com.duoduo.entity.ShoppingCart;
import com.duoduo.mapper.DishMapper;
import com.duoduo.mapper.SetmealMapper;
import com.duoduo.mapper.ShoppingCartMapper;
import com.duoduo.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *  若添加的是相同商品，只需数据库的number+1；否则，插入一条新的数据
     * @param shoppingCartDTO：含dishId，setmealId，dishFlavor；但每次添加购物车时dishId和setmealId只有一个
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 封装查询条件给shoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 获得当前用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        // 根据查询条件判断当前的商品是否已经存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list != null && list.size() > 0){
            // 如果是相同的商品，只需update让数据库的 number + 1（这里的list其实只可能有一条数据）
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);

        }else {
            // 如果不存在，插入新数据
            // 补充shoppingCart对象的其他属性（name、price、image、number、createTime）
            // 判断本次添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                // 本次添加的是菜品，在菜品表查询相关信息
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else {
                // 本次添加的是套餐，在套餐表查询相关信息
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        // 封装userId作为查询条件
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 减少购物车的一个商品数量
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 设置查询条件，用shoppingCart对象封装
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());  // 别忘了封装 userId

        // 判断数据库中的number是否为1
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart = list.get(0); // 列表中只有一条数据
        if (cart.getNumber() > 1){
            // 若不为1，将number-1
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(cart);
        }else {
            // 若为1，删除数据
            shoppingCartMapper.deletById(cart.getId());
        }



    }
}
